#cmake_minimum_required(VERSION 3.0)
set(BIN_NAME udcp-krfb)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
#set(CMAKE_AUTORCC ON)

#set(CMAKE_SKIP_BUILD_RPATH FALSE)
#set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
#set(CMAKE_INSTALL_RPATH /usr/lib/udcp/kwayland)
#set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
#set_target_properties(${TARGER_NAME} PROPERTIES LINK_FLAGS "-Wl,-rpath, /usr/lib/udcp/kwayland")

find_package(Qt5 COMPONENTS Core Widgets Network)
find_package(DtkCore REQUIRED)
find_package(PkgConfig QUIET)
pkg_check_modules(PipeWire IMPORTED_TARGET libpipewire-0.3)
pkg_check_modules(LibVNCServer IMPORTED_TARGET libvncserver)
#pkg_check_modules(KF5WaylandClient IMPORTED_TARGET KF5WaylandClient)
pkg_check_modules(WaylandClient IMPORTED_TARGET wayland-client)
pkg_check_modules(GioUnix2 IMPORTED_TARGET gio-unix-2.0)

add_definitions(
        #    -DQT_DEPRECATED_WARNINGS
        #    -DQT_DISABLE_DEPRECATED_BEFORE=0x050600
        #    -DQT_USE_QSTRINGBUILDER
        #    -DQT_NO_CAST_TO_ASCII
        #    -DQT_NO_CAST_FROM_ASCII
        #    -DQT_NO_CAST_FROM_BYTEARRAY
        #    -DQT_STRICT_ITERATORS
        #    -DQT_NO_URL_CAST_FROM_STRING
        -DQT_NO_SIGNALS_SLOTS_KEYWORDS
        #    -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT
        )

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})


option(HUAWEI_KLU_PGV "Huawei klu project" 0)
if(HUAWEI_KLU_PGV)
        add_definitions("-DHUAWEI_KLU_PGV")
endif()


include(CheckIncludeFile)
CHECK_INCLUDE_FILE("linux/dma-buf.h" HAVE_LINUX_DMABUF_H)
if (HAVE_LINUX_DMABUF_H)
        add_definitions("-DHAVE_LINUX_DMABUF_H")
endif ()


if (USE_QT_WAYLAND_SCANNER)
        set(qwayland-zkde-screencast-unstable-v1_cpp "")
        set(qwayland-zkde-screencast-unstable-v1_h "")
        set(wayland-zkde-screencast-unstable-v1-protocol_c "")
        set(wayland-zkde-screencast-unstable-v1-client-protocol_h "")
else()
        set(qwayland-zkde-screencast-unstable-v1_cpp xdp/qwayland-zkde-screencast-unstable-v1.cpp)
        set(qwayland-zkde-screencast-unstable-v1_h xdp/qwayland-zkde-screencast-unstable-v1.h)
        set(wayland-zkde-screencast-unstable-v1-protocol_c xdp/wayland-zkde-screencast-unstable-v1-protocol.c)
        set(wayland-zkde-screencast-unstable-v1-client-protocol_h xdp/wayland-zkde-screencast-unstable-v1-client-protocol.h)
endif()

set (krfb_SRCS
        framebuffermanager.cpp
        events.cpp
        main.cpp
        sockethelpers.cpp
        rfbservermanager.cpp
        rfbserver.cpp
        rfbclient.cpp
        invitationsrfbclient.cpp
        pw_framebuffer.cpp
        framebuffer.cpp
        xdpevents.cpp
        xdp/waylandintegration.cpp
        xdp/screencasting.cpp
        ${qwayland-zkde-screencast-unstable-v1_cpp}
        ${wayland-zkde-screencast-unstable-v1-protocol_c}
        xdp/pipewire.c
        debug.cpp
        3rdhelper/qtsingleapplication/qtsingleapplication.cpp
        3rdhelper/qtsingleapplication/qtlocalpeer.cpp
        )

set (krfb_HEADERS
        framebuffer.h
        xdpevents.h
        xdp/waylandintegration.h
        xdp/waylandintegration_p.h
        xdp/screencasting.h
        ${qwayland-zkde-screencast-unstable-v1_h}
        ${wayland-zkde-screencast-unstable-v1-client-protocol_h}
        xdp/pipewire.h
        debug.h
        3rdhelper/qtsingleapplication/qtsingleapplication.h
        3rdhelper/qtsingleapplication/qtlocalpeer.h
        )

if (USE_QT_WAYLAND_SCANNER)
        find_package(ECM REQUIRED NO_MODULE)
        set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

        find_package(QtWaylandScanner REQUIRED)
        ecm_add_qtwayland_client_protocol(krfb_HEADERS
		#                PROTOCOL ${CMAKE_CURRENT_SOURCE_DIR}/plasma-wayland-protocols/screencast.xml
		PROTOCOL /usr/share/plasma-wayland-protocols/screencast.xml
                BASENAME zkde-screencast-unstable-v1
                )

        set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/src/wayland-zkde-screencast-unstable-v1-client-protocol.h PROPERTY SKIP_AUTOGEN ON)
        set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/src/qwayland-zkde-screencast-unstable-v1.cpp PROPERTY SKIP_AUTOGEN ON)
endif()


add_executable (${BIN_NAME}
        ${krfb_SRCS}
        ${krfb_HEADERS}
        )


if (${USE_SELF_KWAYLAND})
        set (KF5WaylandClient_DEP KF5WaylandClient)
        set (KF5WaylandClient_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/../kwayland/include)
        add_dependencies (${BIN_NAME}
                KF5WaylandClient
                )
else ()
        set (KF5WaylandClient_DEP -lKF5WaylandClient)
        #                       PkgConfig::KF5WaylandClient
        set (KF5WaylandClient_INCLUDE /usr/include/KF5)
endif ()

target_link_libraries (${BIN_NAME}
        ${JPEG_LIBRARIES}
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Network
        -lxkbcommon
        ${KF5WaylandClient_DEP}
        PkgConfig::WaylandClient
        PkgConfig::GioUnix2
        PkgConfig::PipeWire
        PkgConfig::LibVNCServer
        ${DtkCore_LIBRARIES}
        )

include_directories(data)
target_include_directories(${BIN_NAME} PUBLIC
        PkgConfig::PipeWire
        ${KF5WaylandClient_INCLUDE}
        ${DtkCore_INCLUDE_DIRS}
        )

install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${BIN_NAME}
        DESTINATION /usr/lib/x86_64-linux-gnu/libexec/
        )

install(PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/org.kde.udcp-krfb.desktop
        DESTINATION /usr/share/applications
        )
